package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileFilter;

import application.Manager;

public class Dialog extends JDialog implements ActionListener, KeyListener {

	/**
     * 
     */
	private static final long serialVersionUID = 6343387611833182395L;

	private JFileChooser fileChooser;

	private JMenuItem loadItem;

	private JMenuItem exitMenuItem;

	private JMenuItem loadHelpItem;

	private javax.swing.JLabel label1;

	private javax.swing.JLabel label2;

	private java.awt.Panel panel1;

	public Dialog(int width, int height) {
		setTitle("Titel");
		setSize(width, height);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);

		initComponents();

		fileChooser = new JFileChooser("resource");
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.setDialogTitle("Datei- Auswaehlen");
		FileFilter file = new FileFilter() {

			@Override
			public String getDescription() {
				return "*.pkt, *.gsi, *.csv";
			}

			@Override
			public boolean accept(File f) {
				return f.getName().toLowerCase().endsWith("pkt")
						|| f.isDirectory();
			}
		};
		fileChooser.setFileFilter(file);
		erstelleMenu();
	}

	private void initComponents() {
		panel1 = new java.awt.Panel();
		label1 = new javax.swing.JLabel();
		label2 = new javax.swing.JLabel();

		label1.setText("label1");

		label2.setText("label2");

		javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(
				panel1);
		panel1.setLayout(panel1Layout);
		panel1Layout
				.setHorizontalGroup(panel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panel1Layout
										.createSequentialGroup()
										.addGroup(
												panel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																label2,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																label1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																90,
																Short.MAX_VALUE))
										.addGap(794, 794, 794)));
		panel1Layout
				.setVerticalGroup(panel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panel1Layout
										.createSequentialGroup()
										.addComponent(label2)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(label1)
										.addContainerGap()));

		add(panel1, java.awt.BorderLayout.CENTER);

	}

	private void erstelleMenu() {
		JMenuBar menuBar = new JMenuBar();

		JMenu fileMenu = new JMenu("Datei");
		menuBar.add(fileMenu);

		JMenu helpMenu = new JMenu("Hilfe");
		menuBar.add(helpMenu);

		loadItem = new JMenuItem("Datei laden");
		loadItem.addActionListener(this);
		fileMenu.add(loadItem);

		loadHelpItem = new JMenuItem("Hilfe laden");
		loadHelpItem.addActionListener(this);
		helpMenu.add(loadHelpItem);

		fileMenu.addSeparator();

		exitMenuItem = new JMenuItem("Beenden");
		exitMenuItem.addActionListener(this);
		fileMenu.add(exitMenuItem);

		setJMenuBar(menuBar);
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == loadItem) {
			openFile();
		} else if (e.getSource() == loadHelpItem) {

		} else if (e.getSource() == exitMenuItem) {
			dispose();
		} else {
			dispose();
		}
	}

	private void openFile() {
		int ret = fileChooser.showOpenDialog(this);
		if (ret == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();

			double t1 = System.currentTimeMillis();

			Manager.getInstance().loadFile(file.getPath());

			double t2 = System.currentTimeMillis();
			double time = (t2 - t1) / 1000.;
			label1.setText(time + "sec");
		} else {
			dispose();
		}
	}

}
